<template>
  <div class="app-container">
    <el-card shadow="never" v-loading="loading">
      <el-row :gutter="5">
        <el-col :span="24" class="main-card-view">
          <el-row>
            <el-card shadow="never" class="top-search-view-card">
              <el-row>
                <el-form :inline="true" :model="searchForm" class="demo-form-inline search-view">
                  <el-form-item label="操作人">
                    <el-input v-model="searchForm.operName" placeholder="请输入操作人"></el-input>
                  </el-form-item>
                  <el-form-item label="">
                      <el-date-picker v-model="timer" type="daterange" range-separator="至" start-placeholder="开始日期"
                        end-placeholder="结束日期" value-format="yyyy-MM-dd">
                      </el-date-picker>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="handleSearch" class="search-button">查询</el-button>
                    <el-button type="success" @click="handleReset" class="reset-button">重置</el-button>
                    </el-button>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="excExport" class="search-button">导出</el-button>
                  </el-form-item>
                </el-form>
              </el-row>
            </el-card>
          </el-row>

          <el-row>
            <el-card shadow="never">
              <el-table :data="tableData" :stripe="true" style="width: 100%">
                <el-table-column prop="title" align="center" label="标题" />
                <el-table-column prop="operName" align="center" label="操作人" />
                <el-table-column prop="operTime" align="center" label="操作时间" />
                <el-table-column prop="operIp" align="center" label="操作ip" />
                <el-table-column prop="operUrl" align="center" label="请求url" />
                <el-table-column prop="requestMethod" align="center" label="请求方法" />
                <el-table-column prop="status" align="center" label="结果" >
                    <template slot-scope="scope">
                        {{scope.row.status==0?'成功':'失败'}}
                    </template>
                </el-table-column>
                <!-- <el-table-column align="center" label="操作" width="200">
                  <template slot-scope="scope">
                    <el-button type="text" class="table-delete-btn"
                      @click="handleDelete(scope.$index, scope.row)"></el-button>
                  </template>
                </el-table-column> -->
              </el-table>
              <XPagination ref="pageInfo" :total="total" :get-data="getList" />
            </el-card>
          </el-row>
        </el-col>
      </el-row>
    </el-card>

  </div>
</template>

<script>
import XPagination from "../../components/xpagination/XPagination";
import * as sysOperLog from "../../api/sysOperLog"
import * as formUtils from "../../utils/formUtils";
import XLSX from 'xlsx';
export default {
  name: "list",
  components: { XPagination },
  data () {
    return {
      searchForm: {
        operName: "",
      },
      timer:[],
      tableData: [],
      loading: false,
      dialogVisible: false,
      total: 0,
      form: {},
      editStatus: "add",
    }
  },
  methods: {
    getList (pageInfo) {
      const param = {
        ...this.searchForm,
        ...pageInfo
      }
      if(this.timer[0] && this.timer[1]){
          [param.startTime, param.endTime] = this.timer
      }
      sysOperLog.queryByPage(param).then(response => {
        this.tableData = response.data.data.rows
        this.total = response.data.data.total
        this.loading = false
      }).catch(error => {
        console.log(error)
      })
    },
    handleDelete (index, data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        healthAwarenessDay.deleteById(data.uid).then(result => {
          this.loading = false
          this.$message.success(result.data.msg)
          this.handleSearch()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    excExport(){
        let sheetData = []
        sheetData = this.tableData.map(item => ({
            '标题': item.title,
            '操作人': item.operName,
            '操作时间': item.operTime,
            '操作ip': item.operIp,
            '请求url': item.operUrl,
            '请求方法': item.requestMethod,
            '结果': item.status==0?'成功':'失败'
        }))
        const wb = XLSX.utils.book_new();
        const ws = XLSX.utils.json_to_sheet(sheetData);
        XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');
        XLSX.writeFile(wb, '日志.xlsx');
    },
    handleSearch () {
      this.$refs.pageInfo.loadData()
    },
    handleReset () {
      this.searchForm.operName=''
      this.timer=[]

    },
  },
  mounted () {
    this.handleSearch()
  }
}
</script>

<style scoped lang="scss"></style>
